package com.cn.dao;

import com.cn.bean.Department;
import com.cn.util.JDBCUtils;
import com.cn.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @Author Zhou Tao
 * @Date 2023/12/6 18:35
 */
public class DepartmentDao {
    /**
     * 添加部门
     */
    public int add(Department department){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            String sql="insert into goods_department values(null,?,?)";
            connection = JDBCUtils.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, department.getDeptName());
            preparedStatement.setString(2, department.getDeptDesc());
            result=preparedStatement.executeUpdate();
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            JDBCUtils.release(connection, preparedStatement, null);
        }
        return result;
    }

    /**
     * 查询部门
     */
    public ArrayList<Department> query(String deptName){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ArrayList<Department> list = new ArrayList<>();
        try {
            StringBuffer stringBuffer=new StringBuffer("select * from goods_department");
            if(!StringUtils.isEmpty(deptName)){
                stringBuffer.append(" where deptName like "+"'%"+deptName+"%'");
            }
            String sql = stringBuffer.toString();
            connection = JDBCUtils.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                Department department= new Department();
                int id = resultSet.getInt("deptId");
                department.setDeptId(id);
                String departmentName = resultSet.getString("deptName");
                department.setDeptName(departmentName);
                String deptDesc = resultSet.getString("deptDesc");
                department.setDeptDesc(deptDesc);
                list.add(department);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            JDBCUtils.release(connection, preparedStatement, resultSet);
        }
        return list;
    }

    /**
     * 删除部门
     */
    public int delete(int id){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            String sql="delete from goods_department where deptId=?";
            connection = JDBCUtils.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            result = preparedStatement.executeUpdate();
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            JDBCUtils.release(connection, preparedStatement, null);
        }
        return result;
    }

    /**
     * 更新部门
     */
    public int update(Department department){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            String sql="update goods_department set deptName=?,deptDesc=? where deptId=?";
            connection = JDBCUtils.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            String deptName = department.getDeptName();
            preparedStatement.setString(1, deptName);
            String deptDesc = department.getDeptDesc();
            preparedStatement.setString(2, deptDesc);
            int id = department.getDeptId();
            preparedStatement.setInt(3, id);
            result = preparedStatement.executeUpdate();
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            JDBCUtils.release(connection, preparedStatement, null);
        }
        return result;
    }
}
